using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Xml.Serialization;

namespace Rogue
{
    public sealed class EntityDatabase
    {
        private Dictionary<string, EntityDef> m_Database = new Dictionary<string, EntityDef>();

        private EntityDatabase()
        {
            //ConsoleWindow.Instance.RegisterFunction("TERRAIN.LIST", CONSOLE_ListDefinitions);
        }

        #region Singleton Hackery
        public static EntityDatabase Instance
            { get { return Nested.instance; } }

        class Nested
        {
            static Nested()
            {
                // Nothing
            }

            internal static readonly EntityDatabase instance = new EntityDatabase();
        }
        #endregion

        public EntityDef GetDefinition(string a_sName)
        {
            if (m_Database.ContainsKey(a_sName))
                return m_Database[a_sName];
            else
            {
                throw new Exception("Cannot find entity definition \"" + a_sName + "\"");
            }
        }

        public bool ContainsDefinition(string a_sName)
        {
            if (m_Database.ContainsKey(a_sName))
                return true;
            else
                return false;
        }

        public void LoadDefinitions()
        {
            
            m_Database.Clear();

            List<EntityDef> definitions = RogueGame.Instance.Content.Load<List<EntityDef>>("EntityDefs");

            foreach (EntityDef def in definitions)
            {
                m_Database.Add(def.ClassName, def);
            }
        }

        #region Console Commands
        private void CONSOLE_ListDefinitions(string[] a_sArgs)
        {
            foreach (KeyValuePair<string, EntityDef> def in m_Database)
            {
                ConsoleWindow.Instance.PrintError(def.Value.ClassName);
            }
        }
        #endregion
    }

   

}